<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<title>网上购票</title>
<link rel="stylesheet" href="resources/css/frozen.css">
<link rel="stylesheet" href="resources/css/main.css">
<script src="resources/lib/zepto.min.js"></script>
<script src="resources/js/frozen.js"></script>
<script src="resources/js/config.js"></script>
<script src="resources/js/main.js"></script>

</head>
<body ontouchstart="">
	<header class="ui-header ui-header-positive ui-border-b">
		<i class="ui-icon-return" onclick="history.back()"></i>
		<h1>会员中心</h1>
	</header>
	<footer class="ui-footer ui-footer-btn">
		<ul id="footer" class="ui-tiled ui-border-t">
			<li data-href="index.html" class="ui-border-r">
				<div>查询购票</div>
			</li>
			<li data-href="order.html" class="ui-border-r">
				<div>订单查询</div>
			</li>
			<li data-href="member.html">
				<div>会员中心</div>
			</li>
		</ul>
	</footer>
	<section class="ui-container">
		<section class="ui-selector">
			<header>
				<h3>会员信息</h3>
			</header>
			<div class="ui-form ui-border-t">
				<form action="#">
					<div class="ui-form-item ui-form-item-show ui-border-b">
						<label for="member-name">用户名</label>
						<input id="member-name" type="text" value="">
					</div>
					<div class="ui-form-item ui-form-item-show ui-border-b">
						<label for="id-card">身份证号</label>
						<input id="id-card" type="text" value="">
					</div>
					<div class="ui-form-item ui-form-item-show ui-border-b">
						<label for="mobile">手机号码</label>
						<input id="mobile" type="text" value="">
					</div>
					<div id="score-container" class="ui-form-item ui-form-item-show ui-border-b" style="display: none;">
						<label for="score">积分</label>
						<input id="score" type="text" value="" readonly>
					</div>
				</form>
			</div>
			<ul class="ui-list ui-list-text ui-list-link ui-border-b">
				<li id="friends" class='ui-border-t'>常用联系人</li>
			</ul>
			<div class="ui-btn-wrap">
				<button id="btn-weixin-bind" class="btn-group ui-btn-lg ui-btn-primary" style="display: none;">
					绑定微信
				</button>
				<button id="btn-update-member-info" class="btn-group ui-btn-lg ui-btn-primary">
					保存修改
				</button>
				<button id="btn-change-password" class="btn-group ui-btn-lg ui-btn-primary">
					修改密码
				</button>
				<button id="btn-weixin-unbind" class="btn-group ui-btn-lg ui-btn-danger" style="display: none;">
					解绑微信
				</button>
				<button id="btn-logout" class="btn-group ui-btn-lg ui-btn-danger" style="display: none;">
					退出登录
				</button>
			</div>
			
			<div id="weixin-bind-notice" class="ui-whitespace" style="display: none;">
				<h1 class="ui-txt-warning">温馨提示：</h1>
				<p class="ui-txt-justify">只有通过微信公众号打开本功能，才可以绑定微信。</p>
				<p class="ui-txt-justify">绑定微信后，将无需重复登录。</p>
			</div>
			
			<div id="loading" class="ui-loading-block hide">
				<div class="ui-loading-cnt">
					<i class="ui-loading-bright"></i>
					<p>努力加载中...</p>
				</div>
			</div>
		</section>
	</section>

<script>
// 检查登录状态
checkLogin();

(function (){

	/* -------------------- 判断是否为微信浏览器 -------------------- */
	
	if(appConfig.isWeixin) {
		if(!!sessionStorage.wechatAcc) {
			$("#btn-weixin-unbind").show();
		} else {
			$("#btn-weixin-bind").show();
			$("#btn-logout").show();
		}
		$("#bind-weixin-notice").show();
	} else {
		$("#btn-logout").show();
	}

	/* -------------------- 根据sessionStorage中的值，初始化界面 -------------------- */
	
	if(!!sessionStorage.message) {
		showSuccessTip(sessionStorage.message);
		sessionStorage.message = "";
	}
	
	/* -------------------- 根据localStorage中的值，初始化界面 -------------------- */
	
	// 加载用户名
	$("#member-name").val(sessionStorage.memberName);
	
	// 加载身份证号
	$("#id-card").val(sessionStorage.idCard=='null' ? '' : sessionStorage.idCard);
	
	// 加载手机号码
	$("#mobile").val(sessionStorage.mobile);
	
	// 加载积分
	$("#score").val(sessionStorage.score);
	
	// 常用联系人按钮点击事件
	$("#friends").tap(function(){
		location.href = "friend.html";
	});
	
	// 根据系统参数，判断是否显示用户积分
	if(!!appConfig.memberScore) {
		$("#score-container").show();
	}
	
	// 绑定微信按钮点击事件
	$("#btn-weixin-bind").tap(function(){
		weixinBind();
	});
	
	// 解绑微信按钮点击事件
	$("#btn-weixin-unbind").tap(function(){
		weixinUnbind();
	});
	
	// 保存修改按钮点击事件
	$("#btn-update-member-info").tap(function(){
		updateMemberInfo();
	});
	
	// 修改密码按钮点击事件
	$("#btn-change-password").tap(function(){
		location.href = "changePassword.html";
	});
	
	// 退出登录按钮点击事件
	$("#btn-logout").tap(function(){
		sessionStorage.clear();
	
		location.href = "login.html";
	});
	
	// 底部主菜单初始化
	initFooter("#footer");
})();

// 绑定微信
function weixinBind() {
	ajax({
		url: appConfig.baseUrl + "/wechat",
		data: {
			type: 8
		},
		failMessage: "绑定失败，请重试",
		success: function(data) {
			hideLoading("#loading");
			
			if(data.success) {
				sessionStorage.wechatAcc = data.wechatAcc;
				$("#btn-weixin-bind").hide();
				$("#btn-weixin-unbind").show();
				$("#btn-logout").hide();
				
				showInfoTip("绑定成功");
			} else {
				showInfoTip("绑定失败");
			}
		}
	});
}

// 解绑微信
function weixinUnbind() {
	ajax({
		url: appConfig.baseUrl + "/wechat",
		data: {
			type: 9,
			wechatAcc: sessionStorage.wechatAcc
		},
		failMessage: "解绑失败，请重试",
		success: function(data) {
			hideLoading("#loading");
			
			if(data.success) {
				sessionStorage.removeItem("wechatAcc");
				$("#btn-weixin-bind").show();
				$("#btn-weixin-unbind").hide();
				$("#btn-logout").show();
				
				showInfoTip("解绑成功");
			} else {
				showInfoTip("解绑失败");
			}
		}
	});
}

// 修改会员信息
function updateMemberInfo() {
	showLoading("#loading");
	var friendName = $.trim($("#friend-name").val());
	var idCard = $.trim($("#id-card").val());
	idCard = idCard.toUpperCase(); // 转大写
	$("#id-card").val(idCard);
	var mobile = $.trim($("#mobile").val());
	var action = "edit";
	
	// 身份证有效性校验
	if(!validateIdCard(idCard)) {
		hideLoading("#loading");
		showInfoTip("身份证号验证失败，请检查");
		return;
	}
	
	ajax({
		url: appConfig.baseUrl + "/Member.go",
		data: {
			method: "saveMember",
			memberId: sessionStorage.memberId,
			membername: friendName,
			idcard: idCard,
			mobile: mobile,
			action: action
		},
		failMessage: "保存失败，请重试",
		success: function(data) {
			hideLoading("#loading");
			
			if(!!data.success) {
				if(data.success) {
					history.back();
				} else {
					showInfoTip("身份证号重复");
				}
			} else {
				showInfoTip("保存失败");
			}
		}
	});
}
</script>
</body>
</html>